﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Data;
using System.Data.Common;

namespace DistributedCacheDataProvider
{
    public class DistributedCacheCommand : DbCommand,IDbCommand,IDisposable
    {
        public DistributedCacheCommand()
        {

        }
        public DistributedCacheCommand(String strCommand) 
		{ 
			Debug.WriteLine("DistributedCacheCommand.constructor(str)", "DistributedCacheCommand");
			_CommandText = strCommand;
		}

		public DistributedCacheCommand(String strCommand, DistributedCacheConnection conn) 
		{ 
			Debug.WriteLine("DistributedCacheCommand.constructor(str, conn)", "DistributedCacheCommand");
			_Connection = conn;
			_CommandText = strCommand;
		}

		void NotSupported()
		{
			throw new System.NotSupportedException();
		}

		void NotImpl()
		{
			throw new System.NotImplementedException();
		}

		public override void Cancel() 
		{ 
			Debug.WriteLine("DistributedCacheCommand.Cancel", "DistributedCacheCommand"); 
			NotSupported();
		}

		// SDK sample is a no-op
		public override void Prepare() 
		{ 
			Debug.WriteLine("DistributedCacheCommand.Prepare", "DistributedCacheCommand"); 
		}

		/*
		public override IDbDataParameter CreateParameter()
		{
			Debug.WriteLine("DistributedCacheCommand.CreateParameter", "DistributedCacheCommand");
			NotSupported();
			return null;
		}
		*/

		// this is implemented to give a count of rows affected
		public override int ExecuteNonQuery()
		{ 
			Debug.WriteLine("DistributedCacheCommand.ExecuteNonQuery", "DistributedCacheCommand");

			// must have a valid and open connection 
			if (_Connection == null || _Connection.State != ConnectionState.Open) 
				throw new InvalidOperationException("Connection must valid and open"); 

			int recs;
			DistributedCacheDataReader reader = new DistributedCacheDataReader(_CommandText);
			reader.GetDirectory(_CommandText);
			recs = reader.RecordsAffected;
			reader.Close();
			return recs;
		}

		/*
		public override DbDataReader ExecuteReader()
		{
			Debug.WriteLine("DistributedCacheCommand.ExecuteReader", "DistributedCacheCommand");

			// must have a valid and open connection 
			if (_Connection == null || _Connection.State != ConnectionState.Open) 
				throw new InvalidOperationException("Connection must valid and open"); 

			DistributedCacheDataReader reader = new DistributedCacheDataReader(_CommandText);
			reader.GetDirectory(_CommandText);
			return reader;
		}

		public override DbDataReader ExecuteReader(CommandBehavior b)
		{
			Debug.WriteLine("DistributedCacheCommand.ExecuteReader(b)", "DistributedCacheCommand"); 

			// must have a valid and open connection 
			if (_Connection == null || _Connection.State != ConnectionState.Open) 
				throw new InvalidOperationException("Connection must valid and open"); 

			if ((b & CommandBehavior.KeyInfo) > 0)
				Debug.WriteLine("Behavior includes KeyInfo");

			if ((b & CommandBehavior.SchemaOnly) > 0)
				Debug.WriteLine("Behavior includes SchemaOnly");

			// only implement CloseConnection and "all other"
			if ((b & CommandBehavior.CloseConnection) > 0)
			{
				Debug.WriteLine("Behavior includes CloseConnection");
				DistributedCacheDataReader reader = new DistributedCacheDataReader(_CommandText, _Connection);
				reader.GetDirectory(_CommandText);
				return reader;
			}
			else
			{
				DistributedCacheDataReader reader = new DistributedCacheDataReader(_CommandText);
				reader.GetDirectory(_CommandText);
				return reader;
			}
		}
		*/

		protected override DbDataReader ExecuteDbDataReader(CommandBehavior b)
		{
			Debug.WriteLine("DistributedCacheCommand.ExecuteDbDataReader(b)", "DistributedCacheCommand");

			// must have a valid and open connection 
			if (_Connection == null || _Connection.State != ConnectionState.Open)
				throw new InvalidOperationException("Connection must valid and open");

			if ((b & CommandBehavior.KeyInfo) > 0)
				Debug.WriteLine("Behavior includes KeyInfo");

			if ((b & CommandBehavior.SchemaOnly) > 0)
				Debug.WriteLine("Behavior includes SchemaOnly");

			// only implement CloseConnection and "all other"
			if ((b & CommandBehavior.CloseConnection) > 0)
			{
				Debug.WriteLine("Behavior includes CloseConnection");
				DistributedCacheDataReader reader = new DistributedCacheDataReader(_CommandText, _Connection);
				reader.GetDirectory(_CommandText);
				return reader;
			}
			else
			{
				DistributedCacheDataReader reader = new DistributedCacheDataReader(_CommandText);
				reader.GetDirectory(_CommandText);
				return reader;
			}
		}

		public override object ExecuteScalar()
		{
			Debug.WriteLine("DistributedCacheCommand.ExecuteScalar", "DistributedCacheCommand");
			NotImpl();
			return null;
		}

		protected override DbParameter CreateDbParameter()
		{
			Debug.WriteLine("DistributedCacheCommand.CreateDbParameter", "DistributedCacheCommand");
			NotImpl();
			return null;
		}

		string _CommandText = "C:\\";
		public override string CommandText
		{
			get { 
				Debug.WriteLine("DistributedCacheCommand.get_CommandText", "DistributedCacheCommand");
				return _CommandText;
			}
			set {
				Debug.WriteLine("DistributedCacheCommand.set_CommandText", "DistributedCacheCommand"); 
				_CommandText = value;
			}
		}

		// any value but zero should throw not supported (from SDK)
		int _CommandTimeout = 0;
		public override int CommandTimeout
		{
			get 
			{ 
				Debug.WriteLine("DistributedCacheCommand.get_CommandTimeout", "DistributedCacheCommand");
				return _CommandTimeout;
			}
			set 
			{ 
				Debug.WriteLine("DistributedCacheCommand.set_CommandTimeout", "DistributedCacheCommand");
				if (value != 0)
					NotSupported();
				_CommandTimeout = value;
			}
		}

		CommandType _CommandType;
		public override CommandType CommandType
		{
			get 
			{
				Debug.WriteLine("DistributedCacheCommand.get_CommandType", "DistributedCacheCommand"); 
				return _CommandType;
			}
			set 
			{ 
				// only need to support text per SDK
				Debug.WriteLine("DistributedCacheCommand.set_CommandType", "DistributedCacheCommand");
				if (value != CommandType.Text)
					NotSupported();
				_CommandType = value;
			}
		}

		DbConnection _Connection;
		protected override DbConnection DbConnection
		{
			get
			{
				Debug.WriteLine("DistributedCacheCommand.get_Connection", "DistributedCacheCommand");
				return _Connection;
			}
			set
			{
				Debug.WriteLine("DistributedCacheCommand.set_Connection", "DistributedCacheCommand");
				_Connection = value;
			}
		}

		DbParameterCollection _Parameters = null;
		protected override DbParameterCollection DbParameterCollection
		{
			get { 
				Debug.WriteLine("DistributedCacheCommand.set_Parameters", "DistributedCacheCommand");
				NotSupported();
				return _Parameters;
			}
		}
	
		DbTransaction _Transaction;
		protected override DbTransaction DbTransaction
		{
			get 
			{ 
				Debug.WriteLine("DistributedCacheCommand.get_Transaction", "DistributedCacheCommand");
				NotSupported();
				return _Transaction;
			}
			set 
			{ 
				Debug.WriteLine("DistributedCacheCommand.set_Transaction", "DistributedCacheCommand");
				NotSupported();
				_Transaction = value;
			}
		}
		

		// this doesn't throw an error even though its unused
		bool _DesignTimeVisible = false;
		public override bool DesignTimeVisible
		{
			get
			{
				Debug.WriteLine("DistributedCacheCommand.get_DesignTimeVisible", "DistributedCacheCommand");
				return _DesignTimeVisible;
			}
			set
			{
				Debug.WriteLine("DistributedCacheCommand.set_DesignTimeVisible", "DistributedCacheCommand");
				_DesignTimeVisible = value;
			}
		}

		// this doesn't throw an error even though its unused
		UpdateRowSource _Source = UpdateRowSource.None;
		public override UpdateRowSource UpdatedRowSource
		{
			get { 
				Debug.WriteLine("DistributedCacheCommand.get_UpdatedRowSource", "DistributedCacheCommand");
				return _Source;
			}
			set { 
				Debug.WriteLine("DistributedCacheCommand.set_UpdatedRowSource", "DistributedCacheCommand");
				_Source = value;
			}
		}

    }
}
